<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全选反选</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>
    <input type="checkbox">111<br>
    <input type="checkbox">222<br>
    <input type="checkbox">333<br>
    <input type="checkbox">444<br>
    <input type="checkbox">555<br>
    <input type="checkbox">666<br>
    <input type="checkbox" id="btn1">全选<br>
    <button id="btn2">反选</button>
</body>
<script>
    //全选
    $("#btn1").change(function () {
        var flag = $(this).prop('checked')
        $(':checkbox').prop('checked', flag)
    })
    //反选
    $('#btn2').click(function () {
        var obj = $(':checkbox')
        for (i = 0; i < obj.length - 1; i++) {
            var flag = $(obj[i]).prop('checked')
            $(obj[i]).prop('checked', !flag)
        }
    })
    $(':checkbox').change(function () {
        var obj = $(':checkbox')
        var flag = true
        for (i = 0; i < obj.length-1; i++) {
            if (!$(obj[i]).prop('checked')) {
                flag = false
                break;
            }
        }
        $('#btn1').prop('checked', flag)
    })
</script>

</html>